<p>This rule raises an issue when <code>Array.prototype.map()</code> is immediately followed by <code>Array.prototype.flat()</code> with no arguments
or a depth of 1.</p>
<h2>Why is this an issue?</h2>
<p>JavaScript’s <code>Array.prototype.flatMap()</code> method combines the functionality of <code>map()</code> and <code>flat()</code> in a single
operation. When you chain <code>.map().flat()</code> or <code>.map().flat(1)</code>, you’re performing two separate operations that could be done more
efficiently and clearly with one.</p>
<p>The <code>flatMap()</code> method was specifically designed for this common pattern. It maps each element using a mapping function, then flattens
the result by one level. This approach has several advantages:</p>
<ul>
  <li> <strong>Better performance</strong>: <code>flatMap()</code> avoids creating an intermediate array that <code>map()</code> would produce before
  <code>flat()</code> processes it. </li>
  <li> <strong>Clearer intent</strong>: The method name explicitly communicates that you’re both mapping and flattening. </li>
  <li> <strong>More concise</strong>: One method call instead of two chained calls reduces code complexity. </li>
  <li> <strong>Standard practice</strong>: Using the dedicated method follows JavaScript best practices and makes code more maintainable. </li>
</ul>
<p>The pattern <code>.map().flat()</code> is essentially reimplementing what <code>flatMap()</code> already does optimally.</p>
<h3>What is the potential impact?</h3>
<p>Using <code>.map().flat()</code> instead of <code>.flatMap()</code> creates unnecessary intermediate arrays, which can impact memory usage and
performance, especially with large datasets. It also makes the code less readable and goes against JavaScript best practices.</p>
<h3>How to fix?</h3>
<p>Replace the chained <code>.map().flat()</code> calls with a single <code>.flatMap()</code> call. The mapping function remains the same.</p>
<h4>Non-compliant code example</h4>
<pre data-diff-id="1" data-diff-type="noncompliant">
const result = items.map(item =&gt; transform(item)).flat(); // Noncompliant
</pre>
<h4>Compliant code example</h4>
<pre data-diff-id="1" data-diff-type="compliant">
const result = items.flatMap(item =&gt; transform(item));
</pre>
<h3>Documentation</h3>
<ul>
  <li> <a href="https://github.com/sindresorhus/eslint-plugin-unicorn#readme">eslint-plugin-unicorn</a> - Rule <a
  href="https://github.com/sindresorhus/eslint-plugin-unicorn/blob/HEAD/docs/rules/prefer-array-flat-map.md">prefer-array-flat-map</a> </li>
  <li> MDN - Array.prototype.flatMap() - <a
  href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap">Official documentation for the flatMap()
  method</a> </li>
  <li> MDN - Array.prototype.flat() - <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat">Official
  documentation for the flat() method</a> </li>
  <li> MDN - Array.prototype.map() - <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map">Official
  documentation for the map() method</a> </li>
</ul>
